package com.example.starapp;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;

import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.example.starapp.luckfrag.LuckFragment;
import com.example.starapp.mefrag.MeFragment;
import com.example.starapp.parnterfrag.ParnterFragment;
import com.example.starapp.starfrag.StarFragment;
import com.example.starapp.starfrag.StarInfo;
import com.example.starapp.utils.AssetsUtils;
import com.google.gson.Gson;

import java.util.List;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private static final String TAG = "MainActivity";
    RadioGroup mainRg;
    FragmentManager fm;
    FragmentTransaction tran;
    RadioButton main_rb_me;
    Fragment startFrag,parnterFrag,luckFrag,meFrag;//放入的四个fragment;
    private List<StarInfo.StarinfoBean> list;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //查找控件
        mainRg=findViewById(R.id.main_rg);
        main_rb_me=findViewById(R.id.main_rb_me);
        mainRg.setOnCheckedChangeListener(this);//添加监听器,监听哪个被点击
        StarInfo starInfo = loadData();
        Bundle bundle=new Bundle();
        Bundle bundle_name=new Bundle();
        bundle.putSerializable("info",starInfo);
        startFrag=new StarFragment();
        startFrag.setArguments(bundle);
        parnterFrag=new ParnterFragment();
        parnterFrag.setArguments(bundle);
        luckFrag=new LuckFragment();
        luckFrag.setArguments(bundle);
        meFrag=new MeFragment();


        //将四个Fragment加入到要替换的布局中
        //1.创建碎片管理
        fm = getSupportFragmentManager();
        //碎片处理对象

        tran = fm.beginTransaction();
        //将四个添加
        tran.add(R.id.main_layout_center,startFrag);
        tran.add(R.id.main_layout_center,parnterFrag);
        tran.add(R.id.main_layout_center,luckFrag);
        tran.add(R.id.main_layout_center,meFrag);
        //隐藏三个
        tran.hide(parnterFrag);
        tran.hide(luckFrag);
        tran.hide(meFrag);
        //提交事务
        tran.commit();
        Intent intent = getIntent();
        int id = intent.getIntExtra("id", 0);
        String star_name = intent.getStringExtra("star_name");
        String star_logo_name = intent.getStringExtra("star_logo_name");
        Log.d(TAG, "star_logo_name: "+star_logo_name);
        Log.d(TAG, "star_name: "+star_name);
        Log.d(TAG, "id: "+id);
        bundle_name.putString("name",star_name);
        bundle_name.putString("show_logo_name",star_logo_name);
        meFrag.setArguments(bundle_name);
        if (id == 1) {
            tran.hide(parnterFrag);
            tran.hide(luckFrag);
            tran.hide(startFrag);
            tran.show(meFrag);
            main_rb_me.setChecked(true);
        }

    }
    public StarInfo loadData(){
        //        读取assets/xzcontent/xzcontentlogo.json文件
        String json = AssetsUtils.getJsonByAssets(this, "xzcontent/xzcontent.json");
//        将json字符串转换成创建类的对象
        Gson gson = new Gson();
        StarInfo starInfo = gson.fromJson(json, StarInfo.class);
        //将assets文件夹操作
        AssetsUtils.saveLogoBitmapFromAssets(this,starInfo);
        return  starInfo;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction tran = fm.beginTransaction();
        switch (checkedId) {
            case R.id.main_rb_star:
                tran.hide(parnterFrag);
                tran.hide(luckFrag);
                tran.hide(meFrag);
                tran.show(startFrag);
                break;
            case R.id.main_rb_parnter:
                tran.hide(startFrag);
                tran.hide(luckFrag);
                tran.hide(meFrag);
                tran.show(parnterFrag);
                break;
            case R.id.main_rb_luck:
                tran.hide(startFrag);
                tran.hide(parnterFrag);
                tran.hide(meFrag);
                tran.show(luckFrag);
                break;
            case R.id.main_rb_me:
                tran.hide(startFrag);
                tran.hide(luckFrag);
                tran.hide(parnterFrag);
                tran.show(meFrag);
                break;
        }
        tran.commit();
    }

}
